<template>
  <div class="my-tab-bar">
    <div
      class="tab-item"
      :class="{ active: active === index }"
      v-for="(item, index) in menus"
      :key="item.index"
      @click="selMenu(index, item.cp)"
    >
      <!-- 图标 -->
      <span class="iconfont" :class="item.icon"></span>
      <!-- 文字 -->
      <span>{{ item.name }}</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    menus: {
      type: Array,
      default: () => [],
      required: true,
      // 需求:数据源的限制(validator):tabbar的item项最少2个,最多5个
      // 注意:函数要写逻辑并返回true/false
      validator (arr) {
        console.log('获取父组件的数据:', arr)
        if (arr.length >= 2 && arr.length <= 5) {
          return true
        } else {
          console.error(
            'tabbar的item项最少2个,最多5个'
          )
          return false
        }
      }
    }
  },
  data () {
    return {
      // 当前选中菜单的索引=>作为曹丹高亮的标识flag
      active: 0
    }
  },
  methods: {
    selMenu (index, tagName) {
      this.active = index
      console.log(tagName)
      this.$emit('change-page', tagName)
    }
  }
}
</script>

<style lang="less" scoped>
.my-tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: white;
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    &.active {
      color: chartreuse;
    }
  }
}
</style>
